<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Flow extends Model
{
    protected $guarded = [];
    public static function getFlowList($scene_id=null,$type=null,$date=null){
        $scene_id=$scene_id?$scene_id:Scene::where('is_docking',1)->first()->id;
        $type = $type?$type:'day';
        $file = ['date','time','in','out','num'];
        if($type=='day'){
            if($date==null){
                if(self::where('date',Carbon::today()->toDateString())->first()){
                    $date=Carbon::today()->toDateString();
                }else{
                    $date = self::max('date');
                }
            }else
                $date = $date;
            $where = [
                ['time','<>',0],
                ['scene_id',$scene_id],
                ['date',$date],
            ];
        }elseif ($type=='week'){
            $where =[
                ['time',0],
                ['scene_id',$scene_id],
                ['date','>=',Carbon::now()->startOfWeek()->toDateString()],
            ];
        }elseif ($type=='month'){
            $where =[
                ['time',0],
                ['scene_id',$scene_id],
                ['date','>=',Carbon::now()->startOfMonth()->toDateString()],
            ];
        }elseif ($type=='year'){
            $where =[
                ['time',0],
                ['scene_id',$scene_id],
                ['date','>=',Carbon::now()->startOfYear()->toDateString()],
            ];
        }
        $list = Flow::where($where)->get($file);
        foreach ($list as $v){
            $v->time = config('options.flow_hour')[$v->time];
        }
        return $list;
    }

    public static function getTotalFlow($scene_id=null,$type='day',$date=null){
        $date = $date==null?Carbon::today()->toDateString():$date;
        $arg = [
            'day'=>Carbon::today()->toDateString(),
            'week'=>Carbon::now()->startOfWeek()->toDateString(),
            'month'=>Carbon::now()->startOfMonth()->toDateString(),
            'year'=>Carbon::now()->startOfYear()->toDateString()
        ];
        if($scene_id==null){
            if($type=='day'&&$date!=null){
                $where=[
                    ['date',$date],
                    ['time',0]
                ];
            }else{
                $where=[
                    ['date','>=',$arg[$type]],
                    ['time',0]
                ];
            }
        }else{
            if($type=='day'&&$date!=null){
                $where=[
                    ['date',$date],
                    ['time',0],
                    ['scene_id',$scene_id]
                ];
            }else{
                $where=[
                    ['date','>=',$arg[$type]],
                    ['time',0],
                    ['scene_id',$scene_id]
                ];
            }
        }
        return self::where($where)->sum('num');
    }
    public static function getTimeRangeTotal($scene_id=null,$time=null){
        if($scene_id==null){
            $where=[
                ['date','like','%'.$time.'%'],
                ['time',0],
            ];
        }else{
            $where=[
                ['date','like','%'.$time.'%'],
                ['time',0],
                ['scene_id',$scene_id]
            ];
        }
        return self::where($where)->sum('num');
    }
}